<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,index) in goodsList" :key='index' @click='goToGoodsDetail(goods)'>
				<my-goods :goods='goods'></my-goods>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				params: {
					query: '',
					cid: '',
					pagesize: 10,
					pagenum: 1
				},
				total: 0,
				goodsList: [],
				isLoading: false
			}
		},
		onLoad(options) {
			this.params.query = options.query || ''
			this.params.cid = options.cid || ''
			this.getGoodsList()
		},
		// 上拉加载
		onReachBottom() {
			const {
				pagenum,
				pagesize
			} = this.params
			if (pagenum * pagesize >= this.total) {
				return uni.$showMsg('数据加载完毕')
			}
			if (!this.isLoading) {
				this.params.pagenum++
				// 重新加载数据
				this.getGoodsList()
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			// 清空数据
			this.goodsList = []
			this.params.pagenum = 1
			// 重新加载数据
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async getGoodsList(callback) {
				// 打开节流阀
				this.isLoading = true
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/search', this.params)
				// console.log(res)
				if (meta.status !== 200) {
					return uni.$showMsg()
				}
				// 关闭节流阀
				this.isLoading = false
				this.goodsList = [...this.goodsList, ...message.goods]
				this.total = message.total
				// 关闭下拉刷新
				callback && callback()
			},
			goToGoodsDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			},
		}
	}
</script>

<style lang="scss">

</style>
